package com.guigu.www.erp.tangyingmeng.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.www.erp.common.pojo.DModuleDetails;
import com.guigu.www.erp.common.pojo.DesignModule;
import com.guigu.www.erp.common.pojo.PageVo;
import com.guigu.www.erp.tangyingmeng.dao.DFileMapper;
import com.guigu.www.erp.tangyingmeng.dao.DModuleDetailsMapper;
import com.guigu.www.erp.tangyingmeng.dao.DesignModuleMapper;
import com.guigu.www.erp.tangyingmeng.service.TYMDesignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class TYMDesignServiceimpl implements TYMDesignService {

    @Autowired
    private DesignModuleMapper moduleMapper;
    @Autowired
    private DModuleDetailsMapper detailsMapper;
    @Autowired
    private DFileMapper dFileMapper;


    public int wuliaoinsert(DesignModule designModule,String username){
        //获取编号
        Calendar now = Calendar.getInstance();
        String time="200";
//年
        time+=now.get(Calendar.YEAR);

//月
        String month = (now.get(Calendar.MONTH) + 1)+"";
        month=month.length()==1?"0"+month : month;
        time+=month;

//日
        String day = now.get(Calendar.DAY_OF_MONTH)+"";
        day=day.length()==1?"0"+day : day;
        time+=day;

//查询最大编号的方法
        String s = moduleMapper.queryMaxApplyId();
        if (s==null){
            time+="0001";
        }else {
            Integer i = Integer.parseInt(s.substring(11));
            i++;
            String str=i+"";
            if (str.length()==1){
                str="000"+str;
            }else if (str.length()==2){
                str="00"+str;
            }else if (str.length()==3){
                str="0"+str;
            }
            time+=str;
        }
        designModule.setDesignId(time);
        designModule.setFirstKindId("1");
        designModule.setFirstKindName("电子");
        designModule.setSecondKindId("1");
        designModule.setSecondKindName("计算机");
        designModule.setThirdKindId("1");
        designModule.setThirdKindName("服务器");
        designModule.setDesigner(username);
        designModule.setRegister(username);
        Date date = new Date();
        designModule.setRegisterTime(date.toLocaleString());
        designModule.setCheckTag("0");
        designModule.setChangeTag("0");
        moduleMapper.insertSelective(designModule);
            for(DModuleDetails details : designModule.getDList()){
                details.setParentId(designModule.getDesignModuleId());
                details.setType("物料");
                details.setResidualAmount(details.getAmount());
                detailsMapper.insert(details);
            }
        int upwuliao = dFileMapper.upwuliao(designModule.getProductId());
            return upwuliao;
    }

    @Override
    public PageVo<DesignModule> selshenhe(Integer pageNum,Integer pageSize) {
        Page<DesignModule> page = PageHelper.startPage(pageNum,pageSize);
        List<DesignModule> list = moduleMapper.selshenhe();
        PageInfo<DesignModule> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        List<DesignModule> attenceList = pageInfo.getList();
        PageVo<DesignModule> pageVo = new PageVo<>(total,attenceList);
        return pageVo;
    }

    @Override
    public DesignModule selbyoneshenhe(int designModuleId) {
        DesignModule module = moduleMapper.selbyoneshenhe(designModuleId);
        List<DModuleDetails> details = detailsMapper.selwuliao(designModuleId);
        module.setDList(details);
        return module;
    }

    @Override
    public int cpshenhe(String designId, String userName, String checktag) {
        Date date = new Date();
        DesignModule designModule = new DesignModule();
        designModule.setCheckTag(checktag);
        designModule.setChecker(userName);
        designModule.setCheckTime(date.toLocaleString());
        designModule.setDesignId(designId);
        int cpshenhe = moduleMapper.cpshenhe(designModule);
        return cpshenhe;
    }

    @Override
    public Map<String, Integer> cxshenhe() {
        Map<String,Integer> map = new HashMap<>();
        int weishenhe = moduleMapper.cxshenhe("0");
        int tongguo = moduleMapper.cxshenhe("1");
        int butongguo = moduleMapper.cxshenhe("2");
        map.put("weishenhe",weishenhe);
        map.put("tongguo",tongguo);
        map.put("butongguo",butongguo);
        return map;
    }

    @Override
    public PageVo<DesignModule> selcx(Integer pageNum,Integer pageSize,String productName) {
        Page<DesignModule> page = PageHelper.startPage(pageNum,pageSize);
        List<DesignModule> list = moduleMapper.selcx(productName);
        PageInfo<DesignModule> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        List<DesignModule> attenceList = pageInfo.getList();
        PageVo<DesignModule> pageVo = new PageVo<>(total,attenceList);
        return pageVo;
    }

    @Override
    public int wuliaobg(DesignModule designModule, String username) {
        designModule.setChanger(username);
        Date date = new Date();
        designModule.setChangeTime(date.toLocaleString());
        moduleMapper.wuliaobg(designModule);
        int wuliaobg = detailsMapper.wuliaobg(designModule.getDesignModuleId());
        for(DModuleDetails details : designModule.getDList()){
            details.setParentId(designModule.getDesignModuleId());
            details.setType("物料");
            details.setResidualAmount(details.getAmount());
            detailsMapper.insert(details);
        }
        return wuliaobg;
    }

    @Override
    public PageVo<DesignModule> selbg(Integer pageNum, Integer pageSize) {
        Page<DesignModule> page = PageHelper.startPage(pageNum,pageSize);
        List<DesignModule> list = moduleMapper.selbg();
        PageInfo<DesignModule> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        List<DesignModule> attenceList = pageInfo.getList();
        PageVo<DesignModule> pageVo = new PageVo<>(total,attenceList);
        return pageVo;
    }


}
